﻿Imports System.IO

Module Module1

    Sub Main()

        Dim doc As Object
        Dim StyleLib As Object
        Dim Output As String
        Dim filename As String
        Dim i As Long
        Dim j As Long
        Dim k As Long
        Dim m As Long

        Dim BodyCount As Long
        Dim FaceCount As Long
        Dim StripCount As Long
        Dim VertCount As Long
        Dim VertX As Double
        Dim VertY As Double
        Dim VertZ As Double
        Dim NormX As Double
        Dim NormY As Double
        Dim NormZ As Double
        Dim TexU As Double
        Dim TexV As Double

        Dim VertStr As String
        Dim NormStr As String
        Dim TexStr As String
        Dim temp1 As String
        Dim temp2 As String
        Dim temp3 As String

        Dim BodyStyle As Long
        Dim ConstructionStyle As Long
        Dim FaceStyle As UInteger

        Try


            'set the file name to the passed argument
            filename = System.Environment.GetCommandLineArgs(1)
            If Not File.Exists(filename) Then
                Throw New ApplicationException(filename & " File does not exist")
            End If




            ' set up the object and open the SE file
            doc = CreateObject("SEFacet.FacetReader")
            Call doc.Open(filename)

            StyleLib = CreateObject("SEFacet.StyleLibrary")
            Call StyleLib.Open(filename)

            BodyCount = doc.BodyCount

            Console.WriteLine(CStr(BodyCount) + " bodies in '" + filename + "'")

            For i = 1 To BodyCount
                FaceCount = doc.BodyFaceCount(i)
                Output = "    FaceCount(" + CStr(i) + ") = " + CStr(FaceCount)
                Console.WriteLine(Output)

                BodyStyle = doc.BodyStyle(i)
                ConstructionStyle = doc.BodyConstructionStyle(i)

                OutputStyleInfo(Output, StyleLib, FaceStyle)

                For j = 1 To FaceCount
                    StripCount = doc.BodyFaceStripCount(i, j)
                    Output = "        StripCount(" + CStr(i) + ", " + CStr(j)
                    Output = Output + ") = " + CStr(StripCount)
                    Console.WriteLine(Output)

                    FaceStyle = doc.BodyFaceStyle(i, j)

                    ' If there's no face style, use the body style
                    If (Not StyleLib.StyleExists(FaceStyle)) Then FaceStyle = BodyStyle

                    ' If there's no body style, use the construction style
                    If (Not StyleLib.StyleExists(FaceStyle)) Then FaceStyle = ConstructionStyle

                    ' Output the style ID
                    Output = "        Style ID(" + CStr(i) + ", " + CStr(j)
                    Output = Output + ") = " + CStr(FaceStyle)
                    Console.WriteLine(Output)

                    

                    For k = 1 To StripCount
                        VertCount = doc.BodyFaceStripVertCount(i, j, k)
                        Output = "            VertCount(" + CStr(i) + ", "
                        Output = Output + CStr(j) + ", " + CStr(k) + ") = "
                        Output = Output + CStr(VertCount)
                        Console.WriteLine(Output)

                        VertStr = "Verts(" + CStr(i) + ", " + CStr(j)
                        VertStr = VertStr + ", " + CStr(k) + ") = "
                        NormStr = "Norms(" + CStr(i) + ", " + CStr(j) + ", "
                        NormStr = NormStr + CStr(k) + ") = "
                        TexStr = "UVs(" + CStr(i) + ", " + CStr(j) + ", "
                        TexStr = TexStr + CStr(k) + ") = "

                        For m = 1 To VertCount
                            Call doc.GetBodyFaceStripVertex(i, j, k, m, VertX, VertY, VertZ, NormX, NormY, NormZ, TexU, TexV)

                            temp1 = FormatNumber(VertX, 3)
                            temp2 = FormatNumber(VertY, 3)
                            temp3 = FormatNumber(VertZ, 3)
                            VertStr = VertStr + "(" + temp1 + ", " + temp2
                            VertStr = VertStr + ", " + temp3 + ")"

                            temp1 = FormatNumber(NormX, 3)
                            temp2 = FormatNumber(NormY, 3)
                            temp3 = FormatNumber(NormZ, 3)
                            NormStr = NormStr + "(" + temp1 + ", " + temp2
                            NormStr = NormStr + ", " + temp3 + ")"

                            temp1 = FormatNumber(TexU, 3)
                            temp2 = FormatNumber(TexV, 3)
                            TexStr = TexStr + "(" + temp1 + ", " + temp2 + ")"

                            If m < VertCount Then
                                VertStr = VertStr + ", "
                                NormStr = NormStr + ", "
                                TexStr = TexStr + ", "
                            End If
                        Next

                        Console.WriteLine(VertStr)
                        Console.WriteLine(NormStr)
                        Console.WriteLine(TexStr)
                    Next
                Next
            Next


            Call doc.Close()


        Catch ex As Exception
            Console.WriteLine(ex.Message)

        End Try

        Console.WriteLine("Press any key to close.")
        Console.ReadKey()
    End Sub

    Sub OutputStyleInfo(ByRef output As String, ByVal StyleLib As Object, ByVal FaceStyle As UInteger)
        If StyleLib.StyleExists(FaceStyle) Then

            ' Get the StyleName

            Dim StyleName As String

            StyleName = StyleLib.StyleName(FaceStyle)

            output = "        Style Name(" + CStr(FaceStyle) + ") = " + StyleName
            Console.WriteLine(output)

            ' Get info from StyleSurface

            Dim diffuse As Integer
            Dim specular As Integer
            Dim ambient As Integer
            Dim emission As Integer
            Dim shiny As Single
            Dim alpha As Single

            Call StyleLib.StyleSurface(FaceStyle, diffuse, specular, ambient, emission, shiny, alpha)

            output = "        Style Info(" + CStr(FaceStyle) + ") = Diffuse " + ColorString(diffuse) + ","
            Console.WriteLine(output)
            output = "            Specular " + ColorString(specular) + ", Ambient " + ColorString(ambient) + ","
            Console.WriteLine(output)
            output = "            Emission " + ColorString(emission)
            Console.WriteLine(output)

            output = "        Style Info(" + CStr(FaceStyle) + ") = Shininess (" + CStr(shiny) + "), Alpha (" + CStr(alpha) + ")"
            Console.WriteLine(output)

            ' Get info from StyleSurfaceEx

            Dim reflect As Single
            Dim refract As Single
            Dim lshadows As Integer
            Call StyleLib.StyleSurfaceEx(FaceStyle, reflect, refract, lshadows)

            output = "        Style Info(" + CStr(FaceStyle) + ") = Reflection (" + CStr(reflect) + "), Refraction (" + CStr(refract) + ")"
            Console.WriteLine(output)

            output = "        Style Info(" + CStr(FaceStyle) + ") = "

            Select Case lshadows
                Case 0
                    output = output + "No Shadows"
                Case 1
                    output = output + "Casts Shadows"
                Case 2
                    output = output + "Accepts Shadows"
                Case 3
                    output = output + "Casts and Accepts Shadows"
            End Select

            Console.WriteLine(output)

            ' Get info from StyleWireframe

            Dim WireColor As Integer
            Dim WireWidth As Single
            Dim StipplePattern As Integer
            Dim StippleScale As Integer
            Dim StipplePatternString As String

            Call StyleLib.StyleWireframe(FaceStyle, WireColor, WireWidth, StipplePattern, StippleScale)

            ' Format the hex string for output
            StipplePatternString = Hex(StipplePattern).PadLeft(4, "0")

            StipplePatternString = "&H" + StipplePatternString

            output = "        Wireframe(" + CStr(FaceStyle) + ") = Color " + ColorString(WireColor) + ", Width (" + CStr(WireWidth) + ")"
            Console.WriteLine(output)

            output = "        Wireframe(" + CStr(FaceStyle) + ") = Stipple Pattern (" + StipplePatternString + "), Scale (" + CStr(StippleScale) + ")"
            Console.WriteLine(output)

            ' Get info from StyleTexture

            Dim TextureFile As String = ""
            Dim TextureUnits As Integer
            Dim TextureRotation As Single
            Dim TextureScaleX As Single
            Dim TextureScaleY As Single
            Dim TextureOffsetX As Single
            Dim TextureOffsetY As Single
            Dim Weight As Single
            Dim TextureMirror As Integer
            Dim TextureTrans As Boolean
            Dim BGColor As Integer


            Call StyleLib.StyleTexture(FaceStyle, TextureFile, TextureUnits, TextureRotation, TextureScaleX, TextureScaleY, TextureOffsetX, TextureOffsetY, Weight, TextureMirror, TextureTrans, BGColor)

            If TextureFile <> "" Then

                output = "        Texture(" + CStr(FaceStyle) + ") = " + TextureFile
                Console.WriteLine(output)

                output = "        Texture(" + CStr(FaceStyle) + ") = Rotation (" + CStr(TextureRotation) + ")"
                Console.WriteLine(output)

                output = "        Texture(" + CStr(FaceStyle) + ") = "

                If TextureScaleX = 0 Then
                    output = output + "Surface "
                Else
                    output = output + "World "
                End If

                output = output + "Scale (" + CStr(TextureScaleX) + ", " + CStr(TextureScaleY) + ")"
                Console.WriteLine(output)

                output = "        Texture(" + CStr(FaceStyle) + ") = Offset (" + CStr(TextureOffsetX) + ", " + CStr(TextureOffsetY) + "), Weight (" + CStr(Weight) + ")"
                Console.WriteLine(output)

                output = "        Texture(" + CStr(FaceStyle) + ") = "

                Select Case TextureMirror
                    Case 0
                        output = output + "No Mirror"
                    Case 1
                        output = output + "X Mirror"
                    Case 2
                        output = output + "Y Mirror"
                    Case 3
                        output = output + "X and Y Mirror"
                End Select

                Console.WriteLine(output)

                If TextureTrans Then
                    output = "        Texture(" + CStr(FaceStyle) + ") = Transparent Color " + ColorString(BGColor)
                End If

                Console.WriteLine(output)

            End If

            ' Get info from StyleBumpmap

            Dim BumpFile As String = ""
            Dim BumpUnits As Integer
            Dim BumpRotation As Single
            Dim BumpScaleX As Single
            Dim BumpScaleY As Single
            Dim BumpOffsetX As Single
            Dim BumpOffsetY As Single
            Dim Height As Single
            Dim BumpMirror As Integer

            Call StyleLib.StyleBumpmap(FaceStyle, BumpFile, BumpUnits, BumpRotation, BumpScaleX, BumpScaleY, BumpOffsetX, BumpOffsetY, Height, BumpMirror)

            If BumpFile <> "" Then

                output = "        Bumpmap(" + CStr(FaceStyle) + ") = " + BumpFile
                Console.WriteLine(output)

                output = "        Bumpmap(" + CStr(FaceStyle) + ") = Rotation (" + CStr(BumpRotation) + ")"
                Console.WriteLine(output)

                output = "        Bumpmap(" + CStr(FaceStyle) + ") = "

                If BumpScaleX = 0 Then
                    output = output + "Surface "
                Else
                    output = output + "World "
                End If

                output = output + "Scale (" + CStr(BumpScaleX) + ", " + CStr(BumpScaleY) + ")"
                Console.WriteLine(output)

                output = "        Bumpmap(" + CStr(FaceStyle) + ") = Offset (" + CStr(BumpOffsetX) + ", " + CStr(BumpOffsetY) + "), Weight (" + CStr(Weight) + ")"
                Console.WriteLine(output)

                output = "        Bumpmap(" + CStr(FaceStyle) + ") = "

                Select Case BumpMirror
                    Case 0
                        output = output + "No Mirror"
                    Case 1
                        output = output + "X Mirror"
                    Case 2
                        output = output + "Y Mirror"
                    Case 3
                        output = output + "X and Y Mirror"
                End Select

                Console.WriteLine(output)

            End If

            ' Get Skybox info

            Dim SkyboxType As Integer

            SkyboxType = StyleLib.StyleSkyboxType(FaceStyle)

            If SkyboxType > 0 Then

                output = "        Skybox(" + CStr(FaceStyle) + ") = Type "

                Select Case SkyboxType
                    Case 1
                        output = output + "(Sphere Map)"
                    Case 2
                        output = output + "(Single Image)"
                    Case 3
                        output = output + "(Environment Box)"
                End Select

                Console.WriteLine(output)

                Dim SideCount As Integer
                Dim count As Integer

                SideCount = StyleLib.StyleSkyboxSideCount(FaceStyle)

                For count = 1 To SideCount
                    Dim SideName As String

                    SideName = StyleLib.StyleSkyboxSideFilename(FaceStyle, count)

                    output = "        Skybox(" + CStr(FaceStyle) + "), Side " + CStr(count) + " = (" + SideName + ")"
                    Console.WriteLine(output)
                Next

                If SkyboxType = 3 Then
                    Dim Azimuth As Single
                    Dim Altitude As Single
                    Dim Roll As Single
                    Dim FieldofView As Single

                    Call StyleLib.StyleSkyboxOrientation(FaceStyle, Azimuth, Altitude, Roll, FieldofView)

                    output = "        Skybox Orientation(" + CStr(FaceStyle) + ") = Azimuth (" + CStr(Azimuth) + ")"
                    Console.WriteLine(output)

                    output = "        Skybox Orientation(" + CStr(FaceStyle) + ") = Altitude (" + CStr(Altitude) + ")"
                    Console.WriteLine(output)

                    output = "        Skybox Orientation(" + CStr(FaceStyle) + ") = Roll (" + CStr(Roll) + ")"
                    Console.WriteLine(output)

                    output = "        Skybox Orientation(" + CStr(FaceStyle) + ") = FieldofView (" + CStr(FieldofView) + ")"
                    Console.WriteLine(output)
                End If
            End If
        End If
    End Sub


    Function ColorString(ByVal Color As Long) As String
        Dim red As Integer
        Dim green As Integer
        Dim blue As Integer
        Dim temp As Long

        temp = Color

        red = temp Mod 256
        temp = (temp - red) / 256
        green = temp Mod 256
        temp = (temp - green) / 256
        blue = temp

        ColorString = "(" + CStr(red) + ", " + CStr(green) + ", " + CStr(blue) + ")"
    End Function


End Module
